﻿using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using MvcApplication2.Controllers;

namespace MvcApplication2.Models
{
    public class MvcApplication2Context : DbContext
    {
        public MvcApplication2Context()
                : base("MvcApplication2Db")
            { }
            //public DatabaseContext()
            //    : base(ApplicationParameters.ConnectionStringName)
            //{
            //}

            //public DatabaseContext(string connectionStringName)
            //    : base(connectionStringName)
            //{
            //}


        // You can add custom code to this file. Changes will not be overwritten.
        // 
        // If you want Entity Framework to drop and regenerate your database
        // automatically whenever you change your model schema, add the following
        // code to the Application_Start method in your Global.asax file.
        // Note: this will destroy and re-create your database with every model change.
        // 
        // System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<MvcApplication2.Models.MvcApplication2Context>());

        public DbSet<UserProfile> UsersProfiles { get; set; }

        public DbSet<Exercise> Exercises { get; set; }

        public DbSet<Device> Devices { get; set; }

        public DbSet<Language> Languages { get; set; }

        public DbSet<UserMessage> UserMessages { get; set; }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new UserMessageConfiguration());

            modelBuilder.Ignore<UserMessageViewModel>();
            //modelBuilder.Ignore<ExerciseTestViewModel>();
            //modelBuilder.Entity<Post>()
            //    .HasMany(p => p.Tags)
            //    .WithMany(t => t.Posts)
            //    .Map(mc =>
            //    {
            //        mc.ToTable("PostJoinTag");
            //        mc.MapLeftKey("PostId");
            //        mc.MapRightKey("TagId");
            //    });
            base.OnModelCreating(modelBuilder);
        }
    }

    class UserMessageConfiguration : EntityTypeConfiguration<UserMessage>
    {
        internal UserMessageConfiguration()
        {
            this.HasMany(p => p.UsersAllowed)
                .WithMany(p => p.MessagesAllowed);

            this.HasMany(p => p.ReadByUser)
                .WithMany(p => p.MessageReaded);
        }
    }
}
